<template>
    <div class="login-container">
        <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" label-position="left">
            <div class="title-container">
                <h3 class="title">Login Form</h3>
            </div>
            <el-form-item prop="username">
                <el-input v-model="loginForm.username" placeholder="Username" name="username" tabindex="1" prefix-icon="el-icon-user" />
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" placeholder="Password" tabindex="2" @keyup.enter.native="handleLogin" prefix-icon="el-icon-lock"
                    show-password />
            </el-form-item>
            <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.prevent="handleLogin">Login</el-button>
        </el-form>
    </div>
</template>

<script>
import { mapActions, mapState } from "vuex"
export default {
    name: 'Login',
    data () {
        const validateUsername = (rule, value, callback) => {
            if (value.length < 4) {
                callback(new Error('请输入正确的用户名'))
            } else {
                callback()
            }
        }
        const validatePassword = (rule, value, callback) => {
            if (value.length < 6) {
                callback(new Error('请输入正确的密码'))
            } else {
                callback()
            }
        }
        return {
            loginForm: {
                username: 'admin',
                password: '123456'
            },
            loginRules: {
                username: [{ required: true, trigger: 'blur', validator: validateUsername }],
                password: [{ required: true, trigger: 'blur', validator: validatePassword }]
            },
            loading: false,
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function (route) {
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        handleLogin () {
            this.$refs.loginForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    try {
                        this.login(this.loginForm).then(res => {
                            if (res) {
                                this.$message({
                                    message: '登录成功！',
                                    type: 'success'
                                });
                                setTimeout(() => {
                                    let name = this.authorityList[0].name
                                    console.log(this.authorityList);
                                    console.log(name);
                                    this.$router.push({ name })
                                }, 1000);
                                this.loading = false
                            }
                        })
                    } catch (error) {
                        this.loading = false
                    }
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        ...mapActions('user', ['login'])
    },
    computed: {
        ...mapState('user', ['authorityList'])
    }
}
</script>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
    min-height: 100vh;
    width: 100%;
    background-color: $bg;
    overflow: hidden;
    .login-form {
        position: relative;
        width: 520px;
        max-width: 100%;
        padding: 160px 35px 0;
        margin: 0 auto;
        overflow: hidden;
    }
    .title-container {
        position: relative;
        .title {
            font-size: 26px;
            color: $light_gray;
            margin: 0px auto 40px auto;
            text-align: center;
            font-weight: bold;
        }
    }

    /* 修复input 背景不协调 和光标变色 */
    /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
    @supports (-webkit-mask: none) and (not (cater-color: #fff)) {
        .login-container .el-input input {
            color: #fff;
        }
    }
    ::v-deep .el-input {
        input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            color: #fff;
            height: 47px;
            caret-color: #fff;
            &:-webkit-autofill {
                box-shadow: 0 0 0px 1000px #283443 inset !important;
                -webkit-text-fill-color: #fff !important;
            }
        }
        .el-input__icon {
            line-height: 47px;
            font-size: 18px;
        }
    }

    ::v-deep .el-input--prefix .el-input__inner {
        padding-left: 38px;
    }

    ::v-deep .el-form-item {
        border: 1px solid rgba(255, 255, 255, 0.1);
        background: rgba(0, 0, 0, 0.1);
        border-radius: 5px;
        color: #454545;
    }
}
</style>